﻿using System.Collections.Generic;
using System.Linq;
using PaceR.Core.Interfaces;

namespace PaceR.Core.Entities
{
    public sealed class SimpleResult<T> : IResult<T>
    {
        public SimpleResult(IResultObjectFactory factory)
        {
            _entities = new T[0];
            Messages = factory.GetResultMessagesObject();
        }

        private IEnumerable<T> _entities;

        public T Entity { get { return _entities.FirstOrDefault(); } }

        public IEnumerable<T> Entities { get { return _entities; } }

        public IResultMessages Messages { get; private set; }

        public void SetResultEntities(params T[] entities)
        {
            _entities = entities;
        }

        public void SetResultEntities(IEnumerable<T> entities)
        {
            _entities = entities;
        }

        public void MergeResults(IResult<T> newResult)
        {
            if (newResult != null)
            {
                var entities = new List<T>(this.Entities);
                entities.AddRange(newResult.Entities);
                _entities = entities;
                Messages.MergeMessages(newResult.Messages);
            }
        }
    }
}
